﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ionic.Zip;

namespace DRSLF_robot_
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        Robot robot = new Robot();
        string shap;

        void setSensor(Point p)
        {
            robot.highLightSensor(-1);
            robot.PlaceSensor(new Size((int)nudSensorSizeX.Value, (int)nudSensorSizeY.Value),
                    new Point(p.X, p.Y), (int)nudSenIndex.Value);
            pbRobot.Image = robot.bmirobot;
            lstSensors.Items.Clear();
            foreach (string str in robot.Sensors)
                lstSensors.Items.Add(str);
        }

        private void Main_Load(object sender, EventArgs e)
        {
            robot.PlaceMotor((int)nudMotorDist.Value, (int)nudMotorLong.Value, 250 - (int)tbMotorPlace.Value);
            pbRobot.Image = robot.bmirobot;
        }

        private void btnAddSensor_Click(object sender, EventArgs e)
        {
            shap = "sensor";
            if (robot.Sensors.Count != 0)
                nudSenIndex.Value = int.Parse(robot.Sensors.Last<string>().Split('/')[0]) + 1;
        }

        private void pbRobot_MouseClick(object sender, MouseEventArgs e)
        {
            if (shap == "sensor")
            {
                setSensor(new Point(e.X, e.Y));
                nudSenIndex.Value++;
            }

            if (shap == "move")
            {
                setSensor(new Point(e.X, e.Y));
                robot.highLightSensor((int)nudSenIndex.Value);
                pbRobot.Image = robot.bmirobot;
            }
        }

        private void btnSensorPlace_Click(object sender, EventArgs e)
        {
            setSensor(new Point((int)nudSensorPlaceX.Value, (int)nudSensorPlaceY.Value));
            nudSenIndex.Value++;
        }

        private void lstSensors_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstSensors.SelectedIndex != -1)
            {
                nudSenIndex.Value = int.Parse(lstSensors.SelectedItem.ToString().Split('/')[0]);
                shap = "move";
            }
            else
            {
                robot.highLightSensor(-1);
            }
        }

        private void btnRemoveSensor_Click(object sender, EventArgs e)
        {
            if (lstSensors.SelectedItem == null)
                MessageBox.Show("please choose a sensor for remove.");
            else
            {
                robot.RemoveSensor(int.Parse(lstSensors.SelectedItem.ToString().Split('/')[0]));
                lstSensors.Items.Clear();
                foreach (string str in robot.Sensors)
                    lstSensors.Items.Add(str);
                robot.highLightSensor(-1);
                pbRobot.Image = robot.bmirobot;
            }
        }

        private void nudSenIndex_ValueChanged(object sender, EventArgs e)
        {
            if (robot._Sensors[(int)nudSenIndex.Value] != null)
            {
                robot.highLightSensor((int)nudSenIndex.Value);
            }
            else
            {
                robot.highLightSensor(-1);
            }
            pbRobot.Image = robot.bmirobot;
        }

        void stringToMotor(string[] Line)
        {
            robot.PlaceMotor(int.Parse(Line[1]), int.Parse(Line[0]), 250 - int.Parse(Line[2]));
            nudMotorLong.Value = decimal.Parse(Line[0]);
            nudMotorDist.Value = decimal.Parse(Line[1]);
            tbMotorPlace.Value = 250 - int.Parse(Line[2]);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ofdRobotOpen.ShowDialog() == DialogResult.OK)
            {
                ZipFile zf = ZipFile.Read(ofdRobotOpen.FileName);
                foreach (ZipEntry en in zf)
                {
                    if (en.FileName == "Data.txt")
                        en.Extract(Application.StartupPath, ExtractExistingFileAction.OverwriteSilently);
                }
                zf.Dispose();
                string[] s = System.IO.File.ReadAllLines("Data.txt");
                string[] motorLine = s[0].Split('M');
                stringToMotor(motorLine);
                int i = int.Parse(s[1]);
                for (int j = 2; j <= i + 1; j++)
                {
                    string[] sensorLine = s[j].Split('/');
                    robot.PlaceSensor(new Size((int)nudSensorSizeX.Value, (int)nudSensorSizeY.Value),
                        new Point(int.Parse(sensorLine[1]), int.Parse(sensorLine[2])), int.Parse(sensorLine[0]));
                }
                pbRobot.Image = robot.bmirobot;
            }
        }

        private void exportRobotToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (sfdRobotSaver.ShowDialog() == DialogResult.OK)
            {
                robot.Export(sfdRobotSaver.FileName);
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure about make a new robot? Your datas will be lost.", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                Application.Restart();
            }
        }

        private void pbRobot_MouseMove(object sender, MouseEventArgs e)
        {
            status.Text = "X= " + e.X.ToString() + "mm , Y= " + e.Y.ToString() + "mm";
        }

        private void tbMotorPlace_ValueChanged(object sender, EventArgs e)
        {
            lblMotorPlace.Text = (250-tbMotorPlace.Value).ToString();
            robot.PlaceMotor((int)nudMotorDist.Value, (int)nudMotorLong.Value, 250 - (int)tbMotorPlace.Value);
            pbRobot.Image = robot.bmirobot;
        }

        private void nudMotorDist_ValueChanged(object sender, EventArgs e)
        {
            robot.PlaceMotor((int)nudMotorDist.Value, (int)nudMotorLong.Value, 250 - (int)tbMotorPlace.Value);
            pbRobot.Image = robot.bmirobot;
        }

        private void nudMotorLong_ValueChanged(object sender, EventArgs e)
        {
            robot.PlaceMotor((int)nudMotorDist.Value, (int)nudMotorLong.Value, 250 - (int)tbMotorPlace.Value);
            pbRobot.Image = robot.bmirobot;
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void licenseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            license License = new license();
            License.ShowDialog();
        }
    }
}
